﻿using System;
using System.Collections.Generic;
using System.Text;
using BGShell.PowerShell.Host;
using System.Management.Automation.Host;
using System.Windows.Forms;

namespace BGShell.PowerShell.Controls
{
    partial class ConsoleControl : IRunspaceHostRawUI
    {
        ConsoleColor IRunspaceHostRawUI.BackgroundColor
        {
            get { return ConsoleColor.Black; }
            set { throw new NotSupportedException(); }
        }

        Size IRunspaceHostRawUI.BufferSize
        {
            get
            {
                if (_bufferSize.Width == 0)
                {
                    Invoke(new Action(() =>
                    {
                        const string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
                                           "abcdefghijklmnopqrstuvwxyz";

                        double strWidth = TextRenderer.MeasureText(str, Font).Width;

                        _bufferSize = new Size()
                        {
                            Width = (int)Math.Floor((str.Length * Width) / strWidth),
                            Height = int.MaxValue,
                        };
                    }));
                }

                return _bufferSize;
            }
            set
            {
                _bufferSize = value;
            }
        }

        Coordinates IRunspaceHostRawUI.CursorPosition
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        int IRunspaceHostRawUI.CursorSize
        {
            get { return 1;  }
            set { throw new NotSupportedException(); }
        }

        void IRunspaceHostRawUI.FlushInputBuffer()
        {
        }

        ConsoleColor IRunspaceHostRawUI.ForegroundColor
        {
            get { return ConsoleColor.White;  }
            set { throw new NotSupportedException(); } 
        }

        BufferCell[,] IRunspaceHostRawUI.GetBufferContents(Rectangle rectangle)
        {
            throw new NotImplementedException();
        }

        bool IRunspaceHostRawUI.KeyAvailable
        {
            get { return false; }
        }

        Size IRunspaceHostRawUI.MaxPhysicalWindowSize
        {
            get { throw new NotImplementedException(); }
        }

        Size IRunspaceHostRawUI.MaxWindowSize
        {
            get { throw new NotImplementedException(); }
        }

        KeyInfo IRunspaceHostRawUI.ReadKey(ReadKeyOptions options)
        {
            throw new NotImplementedException();
        }

        void IRunspaceHostRawUI.ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill)
        {
            throw new NotImplementedException();
        }

        void IRunspaceHostRawUI.SetBufferContents(Rectangle rectangle, BufferCell fill)
        {
            throw new NotImplementedException();
        }

        void IRunspaceHostRawUI.SetBufferContents(Coordinates origin, BufferCell[,] contents)
        {
            throw new NotImplementedException();
        }

        Coordinates IRunspaceHostRawUI.WindowPosition
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        Size IRunspaceHostRawUI.WindowSize
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string IRunspaceHostRawUI.WindowTitle
        {
            get { return InvokeIfRequired(() => WindowTitle); }
            set { InvokeIfRequired(() => WindowTitle = value); }
        }
    }
}
